package com.xuwei.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuwei.pojo.User;
import com.xuwei.pojo.Users;
import com.xuwei.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 通过name和 password验证登录
     *
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login(@Param("name") String name, @Param("password") String password, HttpSession session) {

        ModelAndView modelAndView = new ModelAndView();
        User user = userService.login(name, password);
        session.setAttribute("user", user);
        System.out.println("1");
        List<Users> usersList = userService.findUserAll();
        if (user != null) {
            modelAndView.setViewName("success");
            modelAndView.addObject("usersList", usersList);
            return modelAndView;
        }
        modelAndView.setViewName("false");
        return modelAndView;
    }

//    //管理
//    @RequestMapping("/list")
//    public ModelAndView list() {
//        ModelAndView modelAndView = new ModelAndView();
//        List<Users> usersList = userService.findUserAll();
//        modelAndView.addObject("usersList", usersList);
//        modelAndView.setViewName("list");
//        return modelAndView;
//    }

    /**
     * 跳转到添加用户信息页面
     *
     * @return
     */
    @RequestMapping("/toadduser")
    public String addUser() {
        return "add";
    }

    /**
     * 添加用户
     *
     * @param users
     * @return
     */
    @RequestMapping("/adduser")
    public String addUser1(Users users) {
        userService.addUser(users);
        System.out.println("users" + users);
        return "redirect:/user/list";
    }

    /**
     * 跳转到修改用户信息页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/toupdate")
    public String updateUser(Model model, int id) {
        // System.out.println(id);
        Users users = userService.findUserById(id);
        model.addAttribute("users", users);
        return "update";
    }

    /**
     * 修改用户信息
     *
     * @param users
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser1(Users users) {
        userService.updateUser(users);
        System.out.println("users" + users);
        return "redirect:/user/list";
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    public String deleteById(int id) {
        userService.delById(id);
        return "redirect:/user/list";
    }

    /**
     * 分页查询
     *
     * @param map
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String pageList(ModelMap map, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
        Integer pageSize = 5;//每页显示记录数
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        List<Users> usersList = userService.findUserAll();//获取所有用户信息
        PageInfo<Users> pageInfo = new PageInfo<Users>(usersList);
        map.addAttribute("pageInfo", pageInfo);

        return "list";
    }

    /**
     * 搜索加分页
     *
     * @param map
     * @param pageNo
     * @param name
     * @return
     */
    @RequestMapping("/search")
    public String searchMethod(ModelMap map, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo, String name) {
        List<Users> usersList = userService.search(name);//获取搜索后的数据
        Integer pageSize = 5;//每页显示记录数
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Users> pageInfo = new PageInfo<Users>(usersList);
        map.addAttribute("pageInfo", pageInfo);
        return "list";
    }
}
